// **************************************************************
// Com ++ Framework by the C++ World
// Please report defects: mailto:compp@cplusplusworld.com
// Please visit: http://www.cplusplusworld.com/socketlibrary.html
// **************************************************************

#include <iostream>
#include <cstdlib>
#include <string>
#include "ComPlusPlus/ComPlusPlus"

int main (int argc, char *argv [] ) {
	
  if ( argc < 2 ) {
    std::cout << "Usage " << argv[0] << " port [bind-addr]\n";
    return 1;
  }

  int port = std::atoi(argv [1]) ;
  
  std::string host = "0";

  if ( argc == 3 ) {
    host = argv [ 2 ];
  }

  try {
    // -> Comment 1
    compp::SocketUdp Receiver;
    Receiver.SetSockOptBroadcast  ( true );
    Receiver.SetSockOptReuseAddress  ( true );
    Receiver.Bind(host, port);

    do {
      int i;
      char buf[1000];
      std::memset((void*)buf, 0, sizeof(buf));
      std::string host;
      int port;
      // -> Comment 2
      i = Receiver.Read(buf, sizeof(buf), host, port );
      if ( i > 0 )
	std::cout << buf << "\n";	
    } while ( true)  ;
    
    std::cout << "Connection closed\n";
 
  } catch ( syspp::ComException e ) {
    std::cout << "Exception " << e.what() << "\n" ;
  }
  return 0;	
}
